import md5 from 'md5'
//登录注册页 密码自定义加密传输
export function Md5Psd(pwd) {
    const salt = "BodhiNetDisk!";
    return md5(md5(pwd + salt))
}
//获取本地缓存
export function getLocalStorage(key) {
    return localStorage.getItem(key)
}
//设置本地缓存
export function setLocalStorage(key, value) {
    return localStorage.setItem(key, value)
}
//清楚本地缓存
export function clearLocalStorage(key) {
    return localStorage.removeItem(key)
}
//清楚全部本地缓存
export function clearAllLocalStorage(key) {
    return localStorage.clear()
}
//获取本地Session
export function getSession(key) {
    return sessionStorage.getItem(key)
}
//设置本地Session
export function setSession(key, num) {
    return sessionStorage.setItem(key, num)
}
//删除本地Session
export function clearSession(key) {
    return sessionStorage.removeItem(key)
}
//删除全部本地Session
export function clearSessionAll() {
    return sessionStorage.clear()
}
//根据本地是否有token判断是否登录
export function isLogined() {
    if (localStorage.getItem('token')) {
        return true
    }
    return false
}
//将时间戳格式化年月日时分秒
export function timestampFormat(timestamp) {
    let date = new Date(parseInt(timestamp));//时间戳为10位需*1000，时间戳为13位的话不需乘1000
    let year = date.getFullYear(),
        month = ("0" + (date.getMonth() + 1)).slice(-2),
        sdate = ("0" + date.getDate()).slice(-2),
        hour = ("0" + date.getHours()).slice(-2),
        minute = ("0" + date.getMinutes()).slice(-2),
        second = ("0" + date.getSeconds()).slice(-2);
    // 拼接
    let result = year + "-" + month + "-" + sdate + " " + hour + ":" + minute + ":" + second;
    // 返回
    return result;
}
//将时间戳格式化年月日
export function dateFormat(timestamp) {
    let date = new Date(parseInt(timestamp));//时间戳为10位需*1000，时间戳为13位的话不需乘1000
    let year = date.getFullYear(),
        month = ("0" + (date.getMonth() + 1)).slice(-2),
        sdate = ("0" + date.getDate()).slice(-2);
    // 拼接
    let result = year + "年" + month + "月" + sdate + "日";
    // 返回
    return result;
}
//将文件大小转为带单位大小
export function renderSize(filesize) {
    if (filesize === null || filesize === '') {
        return "0 B";
    }
    let unitArr = ["B", "KB", "MB", "GB", "TB", "PB", "EB", "ZB", "YB"]
    let index = 0;
    let srcsize = parseFloat(filesize);
    index = Math.floor(Math.log(srcsize) / Math.log(1024));
    let size = srcsize / Math.pow(1024, index);
    size = size.toFixed(2);//保留的小数位数
    return size + unitArr[index];
}
